package mhe.mgt.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import mhe.mgt.daos.GenericDAOImpl;
import mhe.mgt.model.MchMachinePart;

@SuppressWarnings("unchecked")
public class MachinePartServiceImpl extends GeneralServiceImpl<MchMachinePart> implements MachinePartService {

	@Override
	public Class<MchMachinePart> getEntityClass() {
		return MchMachinePart.class;
	}

	@Override
	public List<MchMachinePart> searchByText(String text) {
		String sql = "select p from MchMachinePart p where p.name like :text or p.note like :text";
		SessionFactory sessionFactory = GenericDAOImpl.instance().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery(sql);
			query.setParameter("text", "%" + text + "%");
			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return new ArrayList<MchMachinePart>(0);
	}

}
